package vn.bus.bk;

import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

import vn.bus.bk.adapter.ListBusAdapter;
import vn.bus.bk.adapter.SQLiteAdapter;
import vn.bus.bk.dialog.DialogMessage;
import vn.bus.bk.object.BusInfo;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class Favorite extends SherlockFragmentActivity {

    private Cursor mCursor;
    private SQLiteAdapter mySQLAdapter;
    private ArrayList<BusInfo> arrayBusFavorite;
    private int iNumber, iGoBus, iBackBus, iName, iTimeStartBus, iTimeEndBus,
	    iFrequency, iValue, iOrdinateGo, iOrdinateBack;
    private String[] columns;
    private SharedPreferences myPreference;
    private ListBusAdapter adapter;
    private ListView listFavorite;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.favorite);
	init();
	setTitle(getString(R.string.menu_item_7));
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	getSupportActionBar().setHomeButtonEnabled(true);
	if (createArrayBus()) {
	    adapter = new ListBusAdapter(this, arrayBusFavorite);
	    listFavorite.setAdapter(adapter);
	} else {
	    DialogMessage dialog = new DialogMessage(this, getString(R.string.empty_favorite));
	    dialog.show(true);
	    
	}

    }

    private void init() {
	// TODO Auto-generated method stub
	myPreference = getSharedPreferences(Menu.FAVORITE, 0);
	mySQLAdapter = new SQLiteAdapter(this);
	columns = getResources().getStringArray(R.array.arrayColumn);
	listFavorite = (ListView) findViewById(R.id.list_favorite);
    }

    private boolean createArrayBus() {
	arrayBusFavorite = new ArrayList<BusInfo>();

	try {
	    mySQLAdapter.close();
	    mySQLAdapter.copyDataBase();
	    mySQLAdapter.openDataBase();

	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    Log.i("DATABASE", "Cannot connect to database");
	}
	try {
	    mCursor = mySQLAdapter.rawAllHN();
	    initialzeIndex();
	    mCursor.moveToFirst();

	    do {
		if (myPreference.getBoolean(
			String.valueOf(mCursor.getString(iName)), false))
		    arrayBusFavorite.add(new BusInfo(mCursor.getInt(iNumber),
			    mCursor.getString(iName),
			    mCursor.getString(iGoBus), mCursor
				    .getString(iBackBus), mCursor
				    .getString(iTimeStartBus), mCursor
				    .getString(iTimeEndBus), mCursor
				    .getString(iFrequency), mCursor
				    .getString(iValue), mCursor
				    .getString(iOrdinateGo), mCursor
				    .getString(iOrdinateBack)));
		mCursor.moveToNext();
	    } while (!mCursor.isAfterLast());

	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	
	try {
	    mCursor = mySQLAdapter.rawAllHCM();
	    initialzeIndex();
	    mCursor.moveToFirst();

	    do {
		if (myPreference.getBoolean(
			String.valueOf(mCursor.getString(iName)), false))
		    arrayBusFavorite.add(new BusInfo(mCursor.getInt(iNumber),
			    mCursor.getString(iName),
			    mCursor.getString(iGoBus), mCursor
				    .getString(iBackBus), mCursor
				    .getString(iTimeStartBus), mCursor
				    .getString(iTimeEndBus), mCursor
				    .getString(iFrequency), mCursor
				    .getString(iValue), mCursor
				    .getString(iOrdinateGo), mCursor
				    .getString(iOrdinateBack)));
		mCursor.moveToNext();
	    } while (!mCursor.isAfterLast());

	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	mySQLAdapter.close();

	if (arrayBusFavorite.size() == 0)
	    return false;
	else
	    return true;

    }
    

    private void initialzeIndex() {
	// TODO Auto-generated method stub
	iNumber = mCursor.getColumnIndex(columns[0]);
	iGoBus = mCursor.getColumnIndex(columns[1]);
	iBackBus = mCursor.getColumnIndex(columns[2]);
	iName = mCursor.getColumnIndex(columns[3]);
	iTimeStartBus = mCursor.getColumnIndex(columns[4]);
	iTimeEndBus = mCursor.getColumnIndex(columns[5]);
	iFrequency = mCursor.getColumnIndex(columns[6]);
	iValue = mCursor.getColumnIndex(columns[7]);
	iOrdinateGo = mCursor.getColumnIndex(columns[8]);
	iOrdinateBack = mCursor.getColumnIndex(columns[9]);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	// TODO Auto-generated method stub
	if (item.getItemId() == android.R.id.home) {
	    finish();
	}
	return super.onOptionsItemSelected(item);
    }

}
